package com.cloudydevelopment.mobiletuner;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

import com.cloudydevelopment.mobiletuner.BluetoothService.LocalBinder;

/*
 * This class displays a UI that the user can use to navigate to different activities.
 */

public class MainActivity extends Activity {

	// Used for debugging
	private static final String TAG = "MainActivity";
	private static final boolean D = true;

	// Intent request code
	private static final int REQUEST_CONNECT_DEVICE_SECURE = 1;
	private static final int REQUEST_ENABLE_BT = 2;

	// Message types sent from the BluetoothService
	public static final int MESSAGE_STATE_CHANGE = 1;
	public static final int MESSAGE_READ = 2;
	public static final int MESSAGE_WRITE = 3;
	public static final int MESSAGE_DEVICE_NAME = 4;
	public static final int MESSAGE_TOAST = 5;

	// Key names received from the BluetoothService
	public static final String DEVICE_NAME = "device_name";

	public static final String TOAST = "toast";

	// Bluetooth Adapter
	private BluetoothAdapter mBluetoothAdapter;

	// Image buttons
	private ImageButton connectButton;
	private ImageButton gaugesButton;

	// Service setup
	private BluetoothService mBluetoothService;
	private boolean mBound = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initConnectButton();
		initGuagesButton();

		// program is checking to see if bluetooth service is supported or
		// enabled.
		// If bluetooth is not supported we get a error message, but if
		// bluetooth is not enabled
		// we get a request to enable bluetooth.
		mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
		if (mBluetoothAdapter == null) {
			Toast.makeText(getApplicationContext(),
					"Bluetooth Service Not Supported!", Toast.LENGTH_SHORT)
					.show();
		} else if (!mBluetoothAdapter.isEnabled()) {
			Intent enableBtIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

		}

	}

	private void initConnectButton() {
		connectButton = (ImageButton) findViewById(R.id.imageButton_Connect);
		connectButton.setOnClickListener(new OnClickListener() {
			Intent intent = null;

			@Override
			public void onClick(View view) {

				intent = new Intent(MainActivity.this,
						DeviceList_Activity.class);
				startActivityForResult(intent, REQUEST_CONNECT_DEVICE_SECURE);
			}

		});
	}

	private void initGuagesButton() {
		gaugesButton = (ImageButton) findViewById(R.id.imageButton_Gauages);
		gaugesButton.setOnClickListener(new OnClickListener() {
			Intent intent = null;

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				intent = new Intent(MainActivity.this, GaugesActivity.class);
				startActivity(intent);
			}

		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (D)
			Log.d(TAG, "onActivityResult " + resultCode);
		switch (requestCode) {
		case REQUEST_CONNECT_DEVICE_SECURE:
			// When DeviceListActivity returns with a device to connect
			if (resultCode == Activity.RESULT_OK) {
				connectDevice(data, true);
			}
			break;
		case REQUEST_ENABLE_BT:
			// When the request to enable Bluetooth returns
			if (resultCode == Activity.RESULT_OK) {

			} else {
				// User did not enable Bluetooth or an error occurred
				Log.d(TAG, "BT not enabled");
				finish();
			}
		}
	}

	private void connectDevice(Intent data, boolean secure) {
		if (D)
			Log.d(TAG, "connectDevice ");
		String address = data.getExtras().getString(
				DeviceList_Activity.EXTRA_DEVICE_ADDRESS);
		if (D)
			Log.d(TAG, address);
		BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
		if (D)
			Log.d(TAG, device.getAddress());
		mBluetoothService.connect(device, true);

	}

	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			mBluetoothService = binder.getService();
			mBound = true;
			Toast.makeText(getApplicationContext(), "Service Bounded",
					Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub
			mBound = false;
			Toast.makeText(getApplicationContext(), "Service unBounded",
					Toast.LENGTH_SHORT).show();
		}

	};

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		if (D)
			Log.e(TAG, " -- ON START -- ");

		if (!mBluetoothAdapter.isEnabled()) {
			Intent enableIntent = new Intent(
					BluetoothAdapter.ACTION_REQUEST_ENABLE);
			startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
		}

		Intent intent = new Intent(this, BluetoothService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		if (D)
			Log.e(TAG, " -- ON PAUSE -- ");
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		if (D)
			Log.e(TAG, " -- ON STOP -- ");
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (D)
			Log.e(TAG, " -- ON DESTROY -- ");
	}

	@Override
	public synchronized void onResume() {
		super.onResume();
		if (D)
			Log.e(TAG, " -- ON RESUME -- ");

	}

}
